! function() {
    var TOUCH_LENGTH = 100;
    var Doc = window.document;

    /*Loading 加载模块 #model-loading*/
    function Loading(id, files, touchMgr) {
        this.el = Doc.querySelector(id);
        this.files = files;
        this.success = function() {
            touchMgr.init();
        };
        this.len = this.files.length;
        this.get = 0;
    }
    Loading.prototype.run = function() {
        for (var i = 0; i < this.len; i++) {
            this.loadImage(this.files[i]);
        }
    };
    Loading.prototype.getOne = function() {
        this.el.style.width = (++this.get / this.len) * 60 + '%';
        console.log('get files:' + this.get);
        if (this.get === this.len) {
            this.success();
        }
    };
    Loading.prototype.loadImage = function(src, callback) {
        var self = this;
        var img = new Image();
        img.onload = function() {
            self.getOne();
        };
        img.src = src;
    };

    /* Page 初始化*/
    function Page(id) {
        this.el = Doc.querySelector(id);
        this.pages = this.el.querySelectorAll('.page');
        this.preHeight = Doc.body.clientHeight;
        this.current = 0;
        this.length = this.pages.length;
        this.managerAnimate = [];
        for (var i = this.pages.length - 1; i >= 0; i--) {
            this.pages[i].style.height = this.preHeight + 'px';
        }
    }
    Page.prototype.next = function() {
        console.log('next');
        this.current++;
        if (this.current > this.length) {
            this.current = this.length;
            return;
        }
        this.showPage('next');
    };
    Page.prototype.prev = function() {
        console.log('prev');
        this.current--;
        if (this.current < 1) {
            this.current = 1;
            return;
        }
        this.showPage('prev');
    };
    Page.prototype.clear = function(arr) {
        if (this.currentPage) {
            this.currentPage.className = this.currentPage.className.replace(' show', ' hide');
        }
        var job = arr.pop();
        while (job) {
            job.el[job.action](job.attr, '');
            job = arr.pop();
        }
    };
    Page.prototype.showPage = function() {
        console.log('show page：' + this.current);
        this.clear(this.managerAnimate);
        this.currentPage = this.el.querySelector('.page-' + this.current);
        if (!this.currentPage) {
            console.log('empty page.');
            return;
        }
        this.currentPage.className = this.currentPage.className.replace(' hide', ' show');
        //this.el.style.transform = 'translate3d(0px, -' + (this.current - 1) * this.preHeight + 'px, 0px)';
        var els = this.currentPage.querySelectorAll('[js-control]');
        var len = els.length;
        for (var i = len - 1; i >= 0; i--) {
            var el = els[i];
            if (el.hasAttribute('js-hide')) {
                setTimeout((function() {
                    this.removeAttribute('js-hide');
                    this.managerAnimate.push({
                        el: this,
                        action: 'setAttribute',
                        attr: 'js-hide'
                    });
                }).bind(el), el.hasAttribute('js-delay') ? parseInt(el.getAttribute('js-delay')) : 0);
            }
            if (el.hasAttribute('js-big')) {
                el.removeAttribute('js-big');
                this.managerAnimate.push({
                    el: el,
                    action: 'setAttribute',
                    attr: 'js-big'
                });
                el.setAttribute('js-big-class', '');
            }
        }
        setTimeout(afterInit, 400);

        function afterInit() {
            for (var i = len - 1; i >= 0; i--) {
                var el = els[i];
                if (el.hasAttribute('js-big-class')) {
                    setTimeout((function() {
                        this.removeAttribute('js-big-class');
                    }).bind(el), el.hasAttribute('js-delay') ? parseInt(el.getAttribute('js-delay')) : 0);
                }
                if (el.hasAttribute('js-animate')) {
                    setTimeout((function() {
                        this.removeAttribute('js-animate');
                        this.managerAnimate.push({
                            el: this,
                            action: 'setAttribute',
                            attr: 'js-animate'
                        });
                        this.setAttribute('js-animate-class', '');
                        this.managerAnimate.push({
                            el: this,
                            action: 'removeAttribute',
                            attr: 'js-animate-class'
                        });
                    }).bind(el), el.hasAttribute('js-delay') ? parseInt(el.getAttribute('js-delay')) : 0);
                }
            }
        };
    };

    /* TouchMgr 初始化*/
    function TouchMgr(page) {
        this.page = page;
        this.start = false;
    }
    TouchMgr.prototype.init = function() {
        var self = this;
        setTimeout(function() {
            Doc.body.className += ' load-ok';
            self.page.next();
        }, 600);
        window.addEventListener('touchstart', touchStart, false);
        window.addEventListener('touchmove', touchMove, false);
        window.addEventListener('touchend', touchEnd, false);

        function touchStart(e) {
            self.start = true;
            var pos = e.changedTouches[0];
            self.clientX = pos.clientX;
            self.clientY = pos.clientY;
        }

        function touchMove(e) {
            if (self.start === true) {
                e.stopPropagation();
                e.preventDefault();
                var pos = e.changedTouches[0];
                self.checkUp(pos);
            }
        }

        function touchEnd(e) {
            if (self.start === true) {
                e.stopPropagation();
                e.preventDefault();
                var pos = e.changedTouches[0];
                self.checkUp(pos);
            }
        }
    };
    TouchMgr.prototype.checkUp = function(pos) {
        var dx = pos.clientX - this.clientX;
        var dy = pos.clientY - this.clientY;
        if (Math.abs(dy) > Math.abs(dx)) {
            if (dy < -TOUCH_LENGTH) {
                this.page.next();
                this.start = false;
            } else if (dy > TOUCH_LENGTH) {
                this.start = false;
                this.page.prev();
            }
        }
    };

    /* Init 初始化*/
    function Init() {
        var page = new Page('#main-page');
        var touchMgr = new TouchMgr(page);
        var load = new Loading('#loading-schedule', CONFIG_files, touchMgr);

        load.run();
    }

    function init() {
        new Init();
    }

    var CONFIG_files = [
        'src/img/logo.png',
        //com
        'src/img/com/c1.png',
        'src/img/com/c2.png',
        'src/img/com/c3.png',
        //end
        'src/img/end/1.jpg',
        'src/img/end/2.jpg',
        'src/img/end/3.jpg',
        //1
        'src/img/1/2_03.png',
        'src/img/1/2_06.png',
        'src/img/1/2_10.png',
        'src/img/1/2_13.png',
        //2
        'src/img/2/2_03.png',
        'src/img/2/2_07.png',
        'src/img/2/2_09.png',
        'src/img/2/2_13.png',
        'src/img/2/2_14.png',
    ];

    window.onload = init;

    (function(doc, win) {
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function() {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
            };
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);
}();